package com.saic.data.dotask;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;

import com.saic.data.table.TableAction;
import com.saic.data.table.TableFile;
import com.saic.data.table.TableUtil;
import com.saic.data.util.HbaseUtil;

public class DeleteTmpRecord extends DoTask{
	private TableFile tfile = null;

	public DeleteTmpRecord(TableFile tfile) {
		this.tfile = tfile;
	}

	@Override
	public void doTask() {

		ResultScanner rs = null;
		try {
			rs = HbaseUtil.QueryByConditionValue(tfile.gettConf().getTmpHtbl(),tfile.gettConf().dataFamily,
					"fileName",tfile.getFileName());			
			int lines = 0;
			for (Result r : rs) {
                boolean flag;
				/** Delete from csv_tmp */
                flag = HbaseUtil.deleteRow(tfile.gettConf().getTmpHtbl(),r.getRow());
				//TableUtil.pm(this.getClass(),"HbaseUtil.deleteRow(Util.tab_csv_tmp, r.getRow()) : "+ flag);
                lines++;
			}

			/** Update FTP file DB */
			long tm = System.currentTimeMillis();
			
			TableUtil.pm(this.getClass(),"File: "+tfile.getFileName() +" Delete Lines : " + lines);
			HbaseUtil.updateData(TableUtil.configtab, tfile.getRowkey().getBytes(),
					TableUtil.configCustomFamily,"oracleRecordLines", lines+"" );
			HbaseUtil.updateData(TableUtil.configtab, tfile.getRowkey().getBytes(),
					TableUtil.configCustomFamily,"oracleRecordTimes", tm + "");
			
			/** Update FTP file Stats */
			HbaseUtil.updateData(TableUtil.configtab, tfile.getRowkey().getBytes(),
					TableUtil.configtabFamily,"fileStats", TableAction.None.toString());

		} catch (Exception e) {
			TableUtil.pm("DeleteTmpRecord-doTask", "Exception");
			e.printStackTrace();
		} finally {
		}
	}

}
